﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Data;
using V5_DBUtility;
using V5_Model;
using System.Data.SqlClient;

namespace V5_DAL.SqlServer
{
    public class DALGZBrand
    {
        #region  Method


        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(Guid ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Bc_Brand");
            strSql.Append(" where ID='" + ID + "' ");
            return DbHelperSQL.Exists(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(ModelGZBrand model)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            if (model.ID != null)
            {
                strSql1.Append("ID,");
                strSql2.Append("'" + Guid.NewGuid().ToString() + "',");
            }
            if (model.BrandNo != null)
            {
                strSql1.Append("BrandNo,");
                strSql2.Append("'" + model.BrandNo + "',");
            }
            if (model.BrandName != null)
            {
                strSql1.Append("BrandName,");
                strSql2.Append("'" + model.BrandName + "',");
            }
            if (model.BrandShortCutName != null)
            {
                strSql1.Append("BrandShortCutName,");
                strSql2.Append("'" + model.BrandShortCutName + "',");
            }
            if (model.Image != null)
            {
                strSql1.Append("Image,");
                strSql2.Append("" + model.Image + ",");
            }

            if (model.PicturePath != null)
            {
                strSql1.Append("PicturePath,");
                strSql2.Append("'" + model.PicturePath + "',");
            }

            strSql1.Append("Oid,");
            strSql2.Append(model.Oid + ",");

            if (model.IsShow != null)
            {
                strSql1.Append("IsShow,");
                strSql2.Append("" + (model.IsShow.Value ? 1 : 0) + ",");
            }
            //if (model.TimeStamp != null) {
            //    strSql1.Append("TimeStamp,");
            //    strSql2.Append("" + model.TimeStamp + ",");
            //}
            strSql.Append("insert into Bc_Brand(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ModelGZBrand model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Bc_Brand set ");
            if (model.BrandNo != null)
            {
                strSql.Append("BrandNo='" + model.BrandNo + "',");
            }
            else
            {
                strSql.Append("BrandNo= null ,");
            }
            if (model.BrandName != null)
            {
                strSql.Append("BrandName='" + model.BrandName + "',");
            }
            else
            {
                strSql.Append("BrandName= null ,");
            }
            if (model.BrandShortCutName != null)
            {
                strSql.Append("BrandShortCutName='" + model.BrandShortCutName + "',");
            }
            else
            {
                strSql.Append("BrandShortCutName= null ,");
            }
            if (model.Image != null)
            {
                strSql.Append("Image=" + model.Image + ",");
            }
            else
            {
                strSql.Append("Image= null ,");
            }

            if (model.PicturePath != null)
            {
                strSql.Append("PicturePath='" + model.PicturePath + "',");
            }
            else
            {
                strSql.Append("PicturePath= null ,");
            }

            strSql.Append("Oid=" + model.Oid + ",");

            if (model.IsShow != null)
            {
                strSql.Append("IsShow=" + (model.IsShow.Value ? 1 : 0) + ",");
            }
            else
            {
                strSql.Append("IsShow= null ,");
            }
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where ID='" + model.ID + "' ");
            int rowsAffected = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(Guid ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Bc_Brand ");
            strSql.Append(" where ID='" + ID + "' ");
            int rowsAffected = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }		/// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Bc_Brand ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ModelGZBrand GetModel(Guid ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1  ");
            strSql.Append(" * ");
            strSql.Append(" from Bc_Brand ");
            strSql.Append(" where ID='" + ID + "' ");
            ModelGZBrand model = new ModelGZBrand();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = new Guid(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["BrandNo"] != null && ds.Tables[0].Rows[0]["BrandNo"].ToString() != "")
                {
                    model.BrandNo = ds.Tables[0].Rows[0]["BrandNo"].ToString();
                }
                if (ds.Tables[0].Rows[0]["BrandName"] != null && ds.Tables[0].Rows[0]["BrandName"].ToString() != "")
                {
                    model.BrandName = ds.Tables[0].Rows[0]["BrandName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["BrandShortCutName"] != null && ds.Tables[0].Rows[0]["BrandShortCutName"].ToString() != "")
                {
                    model.BrandShortCutName = ds.Tables[0].Rows[0]["BrandShortCutName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Image"] != null && ds.Tables[0].Rows[0]["Image"].ToString() != "")
                {
                    model.Image = (byte[])ds.Tables[0].Rows[0]["Image"];
                }
                if (ds.Tables[0].Rows[0]["IsShow"] != null && ds.Tables[0].Rows[0]["IsShow"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsShow"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsShow"].ToString().ToLower() == "true"))
                    {
                        model.IsShow = true;
                    }
                    else
                    {
                        model.IsShow = false;
                    }
                }

                if (ds.Tables[0].Rows[0]["Oid"] != null && ds.Tables[0].Rows[0]["Oid"].ToString() != "")
                {
                    model.Oid = int.Parse(ds.Tables[0].Rows[0]["Oid"]+string.Empty);
                } 

                //if (ds.Tables[0].Rows[0]["TimeStamp"] != null && ds.Tables[0].Rows[0]["TimeStamp"].ToString() != "") {
                //    model.TimeStamp = DateTime.Parse(ds.Tables[0].Rows[0]["TimeStamp"].ToString());
                //}
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM Bc_Brand ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where 1=1 " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" FROM Bc_Brand ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            if (filedOrder != string.Empty)
                strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM Bc_Brand ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.ID desc");
            }
            strSql.Append(")AS Row, T.*  from Bc_Brand T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        */
        public DataSet GetList(string strFiled, int PageSize, int PageIndex, string strWhere, string strOrder, ref int RecordCount)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
            parameters[0].Value = "Bc_Brand";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage", parameters, "ds");
        }
        #endregion  Method
    }
}